package io.renren.modules.medical.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.medical.dao.PatientTreatmentInformationDao;
import io.renren.modules.medical.entity.PatientTreatmentInformationEntity;
import io.renren.modules.medical.entity.history.DrugHistoryEntity;
import io.renren.modules.medical.entity.history.NonDrugHistoryEntity;
import io.renren.modules.medical.entity.history.OperationHistoryEntity;
import io.renren.modules.medical.service.DrugHistoryService;
import io.renren.modules.medical.service.NonDrugHistoryService;
import io.renren.modules.medical.service.OperationHistoryService;
import io.renren.modules.medical.service.PatientTreatmentInformationService;
import io.renren.modules.medical.vo.medicalVo.info.TreatmentInfoSaveVo;
import io.renren.modules.medical.vo.medicalVo.info.TreatmentMapVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service("patientTreatmentInformationService")
public class PatientTreatmentInformationServiceImpl extends ServiceImpl<PatientTreatmentInformationDao, PatientTreatmentInformationEntity> implements PatientTreatmentInformationService {

    private final DrugHistoryService drugHistoryService;

    private final NonDrugHistoryService nonDrugHistoryService;

    private final OperationHistoryService operationHistoryService;

    @Transactional
    @Override
    public void saveTreatment(TreatmentInfoSaveVo treatmentInfoSaveVo) {
        treatmentInfoSaveVo.setId(null);
        PatientTreatmentInformationEntity patientTreatmentInformationEntity = new PatientTreatmentInformationEntity();
        BeanUtils.copyProperties(treatmentInfoSaveVo, patientTreatmentInformationEntity);

        // 先保存三张历史表
        saveHistory(treatmentInfoSaveVo, patientTreatmentInformationEntity);

        this.save(patientTreatmentInformationEntity);
    }

    private void saveHistory(TreatmentInfoSaveVo treatmentInfoSaveVo, PatientTreatmentInformationEntity patientTreatmentInformationEntity) {
        TreatmentMapVo treatmentMap = treatmentInfoSaveVo.getTreatmentMap();
        DrugHistoryEntity drugTreatment = treatmentMap.getDrugTreatment();
        NonDrugHistoryEntity nonDrugTreatment = treatmentMap.getNonDrugTreatment();
        OperationHistoryEntity operationTreatment = treatmentMap.getOperationTreatment();
        if (drugTreatment != null && StringUtils.isNotEmpty(drugTreatment.getDrugName())) {
            drugTreatment.setIsKoa(0);
            drugHistoryService.save(drugTreatment);
            patientTreatmentInformationEntity.setDrugTreatmentId(drugTreatment.getId());
        }
        if (nonDrugTreatment != null && StringUtils.isNotEmpty(nonDrugTreatment.getTreatmentUsage())) {
            nonDrugTreatment.setIsKoa(0);
            nonDrugHistoryService.save(nonDrugTreatment);
            patientTreatmentInformationEntity.setNonDrugTreatmentId(nonDrugTreatment.getId());
        }
        if (operationTreatment != null && StringUtils.isNotEmpty(operationTreatment.getOperationName())) {
            operationTreatment.setIsKoa(0);
            operationHistoryService.save(operationTreatment);
            patientTreatmentInformationEntity.setOperationTreatmentId(operationTreatment.getId());
        }
    }

    @Override
    public void updateTreatment(TreatmentInfoSaveVo treatmentInfoSaveVo) {
        // 先删除三个历史id
        PatientTreatmentInformationEntity treatmentInformationEntity = this.getById(treatmentInfoSaveVo.getId());
        Long drugTreatmentId = treatmentInformationEntity.getDrugTreatmentId();
        Long nonDrugTreatmentId = treatmentInformationEntity.getNonDrugTreatmentId();
        Long operationTreatmentId = treatmentInformationEntity.getOperationTreatmentId();
        if(drugTreatmentId != null){
            this.removeById(drugTreatmentId);
        }
        if (nonDrugTreatmentId != null){
            this.removeById(nonDrugTreatmentId);
        }
        if (operationTreatmentId != null){
            this.removeById(operationTreatmentId);
        }
        PatientTreatmentInformationEntity patientTreatmentInformationEntity = new PatientTreatmentInformationEntity();
        BeanUtils.copyProperties(treatmentInfoSaveVo, patientTreatmentInformationEntity);
        saveHistory(treatmentInfoSaveVo, patientTreatmentInformationEntity);
        this.updateById(patientTreatmentInformationEntity);
    }

    @Override
    public TreatmentInfoSaveVo getTreatmentInfo(Long diseaseId) {
        LambdaQueryWrapper<PatientTreatmentInformationEntity> patientDiseaseInfoWrapper = new LambdaQueryWrapper<>();
        patientDiseaseInfoWrapper.eq(PatientTreatmentInformationEntity::getPatientDiseaseId, diseaseId);
        PatientTreatmentInformationEntity treatmentInformationEntity = this.getOne(patientDiseaseInfoWrapper);
        if (treatmentInformationEntity == null){
            return null;
        }

        TreatmentInfoSaveVo treatmentInfoSaveVo = new TreatmentInfoSaveVo();
        BeanUtils.copyProperties(treatmentInformationEntity, treatmentInfoSaveVo);
        treatmentInfoSaveVo.setTreatmentMap(new TreatmentMapVo());
        Long drugTreatmentId = treatmentInfoSaveVo.getDrugTreatmentId();
        Long nonDrugTreatmentId = treatmentInfoSaveVo.getNonDrugTreatmentId();
        Long operationTreatmentId = treatmentInfoSaveVo.getOperationTreatmentId();
        if (drugTreatmentId != null){
            DrugHistoryEntity drugHistoryEntity = drugHistoryService.getById(drugTreatmentId);
            if (drugHistoryEntity != null){
                treatmentInfoSaveVo.getTreatmentMap().setDrugTreatment(drugHistoryEntity);
            }
        }
        if (nonDrugTreatmentId != null){
            NonDrugHistoryEntity nonDrugHistoryEntity = nonDrugHistoryService.getById(nonDrugTreatmentId);
            if (nonDrugHistoryEntity != null){
                treatmentInfoSaveVo.getTreatmentMap().setNonDrugTreatment(nonDrugHistoryEntity);
            }
        }
        if (operationTreatmentId != null){
            OperationHistoryEntity operationHistoryEntity = operationHistoryService.getById(operationTreatmentId);
            if (operationHistoryEntity != null){
                treatmentInfoSaveVo.getTreatmentMap().setOperationTreatment(operationHistoryEntity);
            }
        }

        return treatmentInfoSaveVo;
    }
}